import {AJAX} from "./ajax.js";
import {URL} from "../router/config.js";
(function () {
    let keyMap = new Map();
    AJAX.get(
        {
            url:URL.HOST_URL()+URL.KEYWORD_URL,
            success:function (data){
                let keyObjects = JSON.parse(data);
                if (keyObjects instanceof Array && keyObjects.length>0){
                    for (const keyObject of keyObjects) {
                        keyObject.keyword = keyObject.keyword.split("|");
                        keyMap.set(keyObject.language,keyObject.keyword);
                    }
                }
            }
        }
    )
    let allCode = document.querySelectorAll(".markdown-code");
    for (const codeElement of allCode) {
        let s = codeElement.innerHTML;
        let language =codeElement.getAttribute("lang");
        let keywords = keyMap.get(language);
        if (keywords instanceof Array){
            for (const keyword of keywords) {
                s = ("  "+s).toString().split(" "+keyword+" ");
                if (s.length>1){
                    s  = s.join("<span class='markdown-keyword glkj-keyword'> "
                        +keyword+" </span>"
                    );
                }else {
                    s=s.join();
                }
                s = s.split("\n"+keyword+" ");
                if (s.length>1){
                    s  = s.join("<span class='markdown-keyword glkj-keyword'><br>"
                        +keyword+" </span>"
                    );
                }else {
                    s=s.join("\n");
                }
            }
            codeElement.innerHTML = s;
        }
    }
})(window);
